package com.woniuxy.handler;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.woniuxy.pojo.OrderDetail;
import com.woniuxy.pojo.RoomType;
import com.woniuxy.pojo.RoomTypocj;
import com.woniuxy.service.RoomTypeService;
import com.woniuxy.util.DateUtil;
@Controller
@RequestMapping("/")
public class RoomTypeHandler {
	OrderDetail orderDetail=new OrderDetail();
	DateUtil dateUtil=new DateUtil();
	@Resource
	private RoomTypeService roomTypeService;
	@RequestMapping("/type")
	@ResponseBody
	//只做一个调转页面和存值的操作
	public ModelAndView findRoomType(HttpServletRequest request) throws ParseException{
		String date = request.getParameter("date");
		System.out.println(date);
		ModelAndView modelAndView=new ModelAndView();
		modelAndView.addObject("date",date);
		modelAndView.setViewName("/findroom.html");
		return modelAndView;
	}
	@RequestMapping("/myfind01")
	@ResponseBody
	public List<RoomType> findRoomType(String date) throws ParseException{
		//获取所有房间类型的个数
		List<RoomTypocj> list1 = roomTypeService.findRoomCount();
		System.out.println(list1);
		//截取入住和退房的日期
		Date startdate=new Date();
		Date enddate=new Date();
		System.out.println(date);
		ArrayList<java.sql.Date> checkTimes = dateUtil.getCheckTimes(date);
		startdate=checkTimes.get(0);//获取入住日期
		enddate=checkTimes.get(1);//获取预计离开时间
		orderDetail.setCheckin(startdate);
		orderDetail.setCheckout(enddate);
		System.out.println(date);
		List<RoomTypocj> list2= roomTypeService.findkzRoomCount(orderDetail);
		System.out.println(list2.size());
		List<RoomTypocj> list3=new ArrayList<>();
		List<RoomType> rooms=new ArrayList<>();
		//两个list进行比较，得到的符合条件的房间类型
		//遍历已住的房间
		if(list2.size()==0){
			list3=list1;
			System.out.println(list3);
		}else{
			for (RoomTypocj list01 : list1) {
				for (RoomTypocj list02 : list2) {
					if(list02.getRtid()!=list01.getRtid()&&list01.getCounts()>list02.getCounts()){
						if(!(list3.contains(list01))){
							list3.add(list01);
						}
						
					}
				}
				
			}
		}
		//通过rtid查询房间的详细信息
		for(int i=0;i<list3.size();i++){
			
			RoomType findRoomTypes = roomTypeService.findRoomTypes(list3.get(i).getRtid());
			System.out.println(findRoomTypes);
			rooms.add(findRoomTypes);
		}
		System.out.println(rooms);
		return rooms;
	}
	//动态sql多条件查询房间类型
	@RequestMapping("/myfind02")
	@ResponseBody
	public List<RoomType> manyFindRoomType(String rwindow,String rfoot,String rsping,int rtid,String finddate) throws ParseException{
		//获取所有房间类型的个数
		List<RoomTypocj> list1 = roomTypeService.findRoomCount();
		System.out.println(list1);
		//截取入住和退房的日期
		Date startdate=new Date();
		Date enddate=new Date();
		ArrayList<java.sql.Date> checkTimes = dateUtil.getCheckTimes(finddate);
		startdate=checkTimes.get(0);//获取入住日期
		enddate=checkTimes.get(1);//获取预计离开时间
		orderDetail.setCheckin(startdate);
		orderDetail.setCheckout(enddate);
		System.out.println(finddate);
		List<RoomTypocj> list2= roomTypeService.findkzRoomCount(orderDetail);
		System.out.println(list2.size());
		List<RoomTypocj> list3=new ArrayList<>();
		List<RoomType> rooms=new ArrayList<>();
		if(list2.size()==0){
			list3=list1;
			System.out.println(list3);
		}else{
			for (RoomTypocj list01 : list1) {
				for (RoomTypocj list02 : list2) {
					if(list02.getRtid()!=list01.getRtid()&&list01.getCounts()>list02.getCounts()){
						if(!(list3.contains(list01))){
							list3.add(list01);
						}
						
					}
				}
				
			}
		}
		//封装房间属性
		RoomType roomType=new RoomType();
		roomType.setRtid(rtid);
		roomType.setRfood(rfoot);
		roomType.setRwindow(rwindow);
		roomType.setRspring(rsping);
			for(int i=0;i<list3.size();i++){
				//判断当前事件
				System.out.println("可以住的id"+list3.get(i).getRtid());
				if(list3.get(i).getRtid()==rtid||rtid==0){
					if(rtid==0){
						roomType.setRtid(0);
					}
					System.out.println(list3.get(i).getRtid());
					System.out.println("if");
					rooms = roomTypeService.findManyRoomType(roomType);
					
				}else {
					System.out.println("没有查询到");
					//rooms=null;
				}
			
		}
		
			System.out.println("查询到的房间"+rooms);
		//通过日期查询到可住的房间类型
		System.out.println(rwindow+":"+rfoot+":"+rsping+":"+rtid+":"+finddate);
		return rooms;
	}
	//查询房间详细信息以及多张图片
	@RequestMapping("/imgs")
	@ResponseBody
	public RoomType findRoomTypeImgs(int rtid){
		System.out.println(rtid);
		System.out.println("11111");
		RoomType roomTypes = roomTypeService.findRoomTypess(rtid);
		System.out.println(roomTypes);
		return roomTypes;
	}
}
